<%@ include file="topo.jsp"%>
<%@ include file="menu.htm"%>
<%@ page import="java.util.ArrayList" %>
<%@ page import="model.Garantia" %>
<%@ page import="model.GarantiaDAO" %>
<%@ page import="model.Contrato" %>
<%@ page import="model.Projeto" %>
<%@ page import="model.Cliente" %>
<%@ page import="model.Bem" %>
<%@ page import="model.BemDAO" %>
<%
// Definir guia destacada
String etapaGarantia;
if (null == request.getAttribute("etapaGarantia"))
	etapaGarantia = "primeira";
else
	etapaGarantia = (String)request.getAttribute("etapaGarantia");
%>
<script>
function definirGarantias(){
	for (var i = 0; i < document.frmListarGarantia.garantias.options.length; i++)
		document.frmListarGarantia.garantias.options[i].selected = true;
}
</script>
<table border="0" cellspacing="0" cellpadding="0" width="800" align="center">
  <tr>
    <td><div class="contexto">Acompanhamento &#8227; Garantia</div></td>
    <td valign="bottom"><div class="fase-<% if (etapaGarantia == "primeira"){ %>atual<% }else{ %>disponivel<% } %>">Pesquisa</div></td>
    <td valign="bottom"><div class="fase-<% if (etapaGarantia == "segunda"){ %>atual<% }else{ %>disponivel<% } %>">Lista</div></td>
    <td valign="bottom"><div class="fase-<% if (etapaGarantia == "terceira"){ %>atual<% }else{ %>disponivel<% } %>">Detalhes</div></td>
  </tr>
</table>
<table width="800" border="0" align="center" height="400" cellspacing="0" background="img/borda.png">
  <tr>
    <td valign="top" align="center">
<%
// Dados recebidos
Contrato contrato = (Contrato)request.getAttribute("contrato");
if (contrato != null) {
	Projeto projeto = contrato.getProjeto();
	Cliente cliente = projeto.getCliente();
	// Auto-post
	ArrayList<Bem> garantias = (ArrayList<Bem>)request.getAttribute("garantias");
%>
    <form name="frmListarGarantia" action="GarantiaControle" method="post">
    <input type="hidden" name="contrato" value="<%= Integer.toString(contrato.getNumero()) %>">
    <table border="0" width="95%">
      <tr>
        <td align="right" width="45%">Cliente: <%= cliente.getRazaoSocial() %></td>
        <td width="10%">&nbsp;</td>
        <td align="left" width="45%">Contrato: <%= Integer.toString(contrato.getNumero()) %></td>
      </tr>
      <tr><td colspan="3"><br><br></td></tr>
      <tr>
        <td align="right">Bens:<br>
            <select name="bens" id="bens" multiple size="10" style="width: 200px">
<%
	if (garantias == null)
		for (Bem bem : projeto.getBens())
			out.println("<option value=\"" + bem.getId() + "\">" + bem.getDescricao() + "</option>");
%>
              </select>
        </td>
        <td align="center">
          <input name="adicionar" type="button" value="&rsaquo;"<% if(garantias == null) { %> onClick="move(this.form.bens,this.form.garantias);"<% } %>>
          <br><br>
          <input name="remover" type="button"value="&lsaquo;"<% if(garantias == null) { %> onClick="move(this.form.garantias,this.form.bens);"<% } %>>
        </td>
        <td align="left">Garantias:<br>
            <select name="garantias" id="garantias" size="10" multiple style="width: 200px">
<%
	if (garantias != null)
		for (Bem b : garantias)
			out.println("<option value=\"" + b.getId() + "\">" + b.getDescricao() + "</option>");
	else {
		GarantiaDAO gd = new GarantiaDAO();
		BemDAO bd = new BemDAO();
		ArrayList<Garantia> lista = gd.consultar(new Garantia(contrato));
		for (Garantia g : lista)
			out.println("<option value=" + g.getBem().getId() + ">" + bd.consultar(g.getBem(),true).get(0).getDescricao() + "</option>");
	}
%>
            </select>
        </td>
      </tr>
      <tr><td colspan="3"><br><br></td></tr>
      <tr><td colspan="3" align="center"><input type="submit" value="Definir" name="operacao" onClick="definirGarantias()"<%= (garantias != null)?" disabled":"" %>> <input type="submit" value="Editar" name="operacao"></td></tr>
    </table>
    </form>
    </td>
  </tr>
</table>
<%
} else {
%>
    Nenhum dado encontrado.</td>
  </tr>
</table>
<%
}
%>
<%@ include file="rodape.htm"%>